package com.ruiysoft.service.wechat.rabbit;

import com.ruiysoft.conf.mq.PayCallBackMqConfig;
import com.ruiysoft.controller.wx.dto.OrderApplyRefundDto;
import com.ruiysoft.controller.wx.dto.OrderPaidDto;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.ruiysoft.conf.mq.PayCallBackMqConfig.PAY_CALLBACK_QUANTITY_BIND_KEY;

/**
 * Created by 化增光 on 2018/11/24.
 */
@Component
public class PayCallbackSender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("消息发送成功:"+correlationData);
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息发送失败:" + new String(message.getBody()));
    }

    public void send(String orderNo,OrderPaidDto message){
        CorrelationData correlationId = new CorrelationData(orderNo);
        rabbitTemplate.convertAndSend(PayCallBackMqConfig.PAY_CALLBACK_QUANTITY_EXCHANGE, PAY_CALLBACK_QUANTITY_BIND_KEY, message, correlationId);
    }

    public void sendVip(String orderNo,OrderPaidDto message){
        CorrelationData correlationId = new CorrelationData(orderNo);
        rabbitTemplate.convertAndSend(PayCallBackMqConfig.PAY_CALLBACK_QUANTITY_EXCHANGE, PayCallBackMqConfig.PAY_CALLBACK_VIP_QUANTITY_BIND_KEY, message, correlationId);
    }

    public void sendRefund(String orderNo, OrderApplyRefundDto message) {
        CorrelationData correlationId = new CorrelationData(orderNo);
        rabbitTemplate.convertAndSend(PayCallBackMqConfig.PAY_CALLBACK_QUANTITY_EXCHANGE, PayCallBackMqConfig.PAY_CALLBACK_VIP_QUANTITY_REFUND_KEY, message, correlationId);
    }
}
